<div class="filter">
    <div class="f1 item">
        <select>
            <option value="0">系统默认值</option>
            <option value="1">代理</option>
        </select>
    </div>
    
    <div class="f2 item">
        <input class="am-form-field" placeholder="请输入代理账号名">
    </div>
    <div class="f12 item">
        <button type="button" class="am-btn am-btn-primary">确定</button>
    </div>
</div>

<table class="am-table daili-table">
    <thead>
        <tr>
            <th width="150px">参数说明</th>
            <th>参数值</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <tr class="global">
            <td>当月有效会员数：</td>
            <td>
                <input type="number" class="am-form-field" name="yxhy" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr class="global">
            <td>达标会员数：</td>
            <td>
                <input type="number" class="am-form-field" name="dabiao" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr class="global">
            <td colspan="3" style="font-weight:bold;">达标会员定义：</td>
        </tr>
        <tr class="global db-charge">
            <td>充值总额：</td>
            <td>
                <input type="number" class="am-form-field" name="db-charge" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr class="global db-bet">
            <td>下注总额：</td>
            <td>
                <input type="number" class="am-form-field" name="db-bet" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr>
            <td colspan="3" style="font-weight:bold;">平台费用平摊</td>
        </tr>
        <tr>
            <td>出款入款手续费、资金风险费用总额</td>
            <td>
                <input type="number" class="am-form-field" name="fengxian_total" placeholder="单位：元" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr class="global">
            <td>出款入款手续费、资金风险费用百分比</td>
            <td>
                <input type="number" class="am-form-field" name="fengxian" placeholder="格式例 百分之一：1" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr>
            <td>行政费用总额</td>
            <td>
                <input type="number" class="am-form-field" name="xingzheng_total" placeholder="单位：元" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr class="global">
            <td>行政费用百分比</td>
            <td>
                <input type="number" class="am-form-field" name="xingzheng" placeholder="格式例 百分之一：0.01" />
            </td>
            <td><button>保存</button></td>
        </tr>
        <tr class="global">
            <td colspan="3" style="font-weight:bold;">
                分红比例，只针对代理模式为“分红”的代理起效，对“赔率差”代理模式不起效。
            </td>
        </tr>
        <tr class="global">
            <td>默认分红比例：</td>
            <td>
                <input type="number" class="am-form-field" name="choucheng" placeholder="格式例 百分之一：1" />
            </td>
            <td><button id="choucheng-btn">保存</button></td>
        </tr>
        <tr class="referee" style="display:none;">
            <td>分红比例：</td>
            <td>
                <input type="number" class="am-form-field" name="referee-fee" placeholder="格式例 百分之一：1。填写0表示使用全局的分红比例" />
            </td>
            <td><button id="referee-fee">保存</button></td>
        </tr>
        <tr class="global">
            <td>纯盈利</td>
            <td>
                <button id="add-cyl">增加纯盈利区间</button>
            </td>
        </tr>
    </tbody>
</table>

<table class="am-table cyl global">
    <thead>
        <tr>
            <th>纯盈利开始</th>
            <th>纯盈利结束(不含)</th>
            <th>分红比例(百分比)</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>
<style type="text/css">
    .cyl td { text-align: center; }
</style>

<script>
    var refereeInfo = null;
    var refereeOper = null;
    if (context.center.refereeBonus == 'referee') {
        refereeOper = 'referee';
        refereeInfo = context.center.refereeInfo;
        $(".f1 select").val(1);
        _switchGlobal();

        $("input[name='fengxian_total']").val(((refereeInfo.riskFee || 0) / 100).toFixed(2));
        $("input[name='xingzheng_total']").val(((refereeInfo.adminFee || 0) / 100).toFixed(2));
        $("input[name='referee-fee']").val(((refereeInfo.bonusRate || 0) * 100).toFixed(2));
        $(".f2 input").val(refereeInfo.mobile);
    }

    $(".f12 button").click(function() {
        _switchGlobal();
        if ($(".f1 select").val() == 0) {
            refereeOper = null;
            context.center.refereeBonus = null;
            _queryGlobal();
        }
        else {
            refereeOper = "referee";
            var uid = $(".f2 input").val().trim();
            if (!uid) {
                alert2("请输入代理账号", true);
                return;
            }
            refereeInfo = null;
            startLoading();
            CMD.get_user_list({
                uid: uid,
            }, 1, 1, function(r) {
                stopLoading();
                if (Util.isEmpty(r.list)) alert2("用户不存在！", true);
                else if (r.list[0].grade != 49) {
                    alert2("此用户不是代理，无法设置！", true);
                }
                else {
                    refereeInfo = r.list[0];
                    alert2("代理账号：" + refereeInfo.mobile + " &nbsp;&nbsp;名字：" + (refereeInfo.name || ''), false);

                    $("input[name='fengxian_total']").val(((refereeInfo.riskFee || 0) / 100).toFixed(2));
                    $("input[name='xingzheng_total']").val(((refereeInfo.adminFee || 0) / 100).toFixed(2));
                    $("input[name='referee-fee']").val(((refereeInfo.bonusRate || 0) * 100).toFixed(2));
                }
            });
        }
    });

    function _switchGlobal() {
        if ($(".f1 select").val() == 0) {
            $(".global").show();
            $(".referee").hide();
        }
        else {
            $(".global").hide();
            $(".referee").show();
        }
    }

    var _querySetting = function(key, name, ratio) {
        Classify.query("global.referee", key, false, 0, function(v) {
            if (ratio !== undefined) v = v * ratio;
            $("input[name='" + name + "']").val(v);
        })
    }
    var _saveSetting = function(key, name, ratio) {
        startLoading();
        var v = $("input[name='" + name + "']").val().trim();
        if (ratio !== undefined) v = v/ratio;
        Classify.save("global.referee", key, v + "", function(v) {
            stopLoading();
            alert2("保存成功！");
        })
    }

    var arr = [ ["admin.fee", "xingzheng_total", 0.01], ["admin.rate", "xingzheng", 100], 
                ["risk.fee", "fengxian_total", 0.01], ["risk.rate", "fengxian", 100],
                ["member.valid", "yxhy"], ["member.std", "dabiao"],
    ];
    function _queryGlobal() {
        for (var i in arr) {
            _querySetting(arr[i][0], arr[i][1], arr[i][2]);
        }

        Classify.query("global.referee", "member.std.deposit", false, 0, function(r) {
            $("input[name='db-charge']").val(r/100);
        });
        Classify.query("global.referee", "member.std.bet", false, 0, function(r) {
            $("input[name='db-bet']").val(r/100);
        });
    }
    _queryGlobal();

    function _saveReferee() {
        if (!refereeInfo) {
            alert2("请选择要设置的代理。", true);
            return;
        }
        // 代理的值
        var data = {
            admin: ($("input[name='xingzheng_total']").val() || 0)*100,
            risk: ($("input[name='fengxian_total']").val() || 0)*100,
            bonus: ($("input[name='referee-fee']").val() || 0)/100,
            id: refereeInfo.id,
        }
        startLoading();
        CMD.user_referee_type_rate(data, function() {
            stopLoading();
            alert2("保存成功", false);
        });
    }

    $(".daili-table button").click(function(e) {
        var name = $(e.currentTarget).parent().parent().find("input").attr("name");
        if (refereeOper == "referee") {
            _saveReferee();
            return;
        }
        for (var i in arr) {
            if (arr[i][1] == name) {
                _saveSetting(arr[i][0], arr[i][1], arr[i][2]);
            }
        }
    });

    $("#referee-fee").click(function() {
        _saveReferee();
    });

    var _doSave = function(e) {
        var tr = $(e.currentTarget).parent().parent();
        var id = tr.attr("value");
        var data = {
            type: 0,
        };
        if (id) {
            data.id = id;
        }
        data.min = (tr.find(".start input").val() || '0') * 100;
        data.max = (tr.find(".end input").val() || '0') * 100;
        data.rate = (tr.find(".rate input").val() || '0') / 100;
        
        startLoading();
        CMD.bonus_rate_save(data, function() {
            stopLoading();
        });
    }
    var _doDelete = function(e) {
        var tr = $(e.currentTarget).parent().parent();
        var id = tr.attr("value");
        tr.remove();
        if (id) {
            CMD.bonus_rate_delete(id);
        }
    }

     // 拉取纯盈利的配置
     var _queryList = function() {
        CMD.bonus_rate_query(function(r) {
            var table = $(".cyl tbody");
            for (var i in r) {
                var d = r[i];
                if (d.type == 1) {
                    $("input[name='choucheng']").val(d.rate * 100);
                    $("input[name='choucheng']").attr("id", d.id);
                    continue;
                }
                var tr = $("<tr value='" + d.id + "'></tr>");
                tr.append("<td class='start'><input type='number' value='" + d.min/100 + "'/></td>");
                tr.append("<td class='end'><input type='number' value='" + d.max/100 + "'/></td>");
                tr.append("<td class='rate'><input type='number' value='" + d.rate * 100 + "'/></td>");
                tr.append("<td class='oper'><button class='save'>保存</button>\
                    <button class='delete'>删除</button>\
                    </td>");
                table.append(tr);
            }
            $(".cyl tbody .save").click(_doSave);
            $(".cyl tbody .delete").click(_doDelete);
        });
    }
    _queryList();

    var cyls = [];
    $("#add-cyl").click(function() {
        var tr = $("<tr></tr>");
        tr.append("<td class='start'><input type='number'/></td>");
        tr.append("<td class='end'><input type='number'/></td>");
        tr.append("<td class='rate'><input type='number'/></td>");
        tr.append("<td class='oper'><button class='save'>保存</button>\
            <button class='delete'>删除</button>\
            </td>");
        $(".cyl tbody").append(tr);

        tr.find(".save").click(_doSave);
        tr.find(".delete").click(_doDelete);
    });

    $("#choucheng-btn").click(function() {
        var v = $("input[name='choucheng']").val() || '0';
        var data = {
            rate: v/100,
            type: 1,
        };
        var v = $("input[name='choucheng']").attr("id");
        if (v) data.id = v;
        startLoading();
        CMD.bonus_rate_save(data, function() {
            stopLoading();

            // 刷新下页面
            Util.refresh();
        });
    })
    

    $(".db-charge button").click(function() {
        var v = $("input[name='db-charge']").val();
        if (v) v = v * 100;
        else v = 0;
        startLoading();
        Classify.save("global.referee", "member.std.deposit", v + "", function() {
            stopLoading();
            alert2("保存成功。");
        })
    });
    $(".db-bet button").click(function() {
        var v = $("input[name='db-bet']").val();
        if (v) v = v * 100;
        else v = 0;
        startLoading();
        Classify.save("global.referee", "member.std.bet", v + "", function() {
            stopLoading();
            alert2("保存成功。");
        })
    });
</script>